package com.loop.ddz;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class PokerTest {
    //随机牌，在谁手里谁就是地主
    static Poker poker;

    public static void main(String[] args) {
        startGame();
    }

    public static void startGame(){
        //给三位玩家发牌，初始化每个人暂时都为农民身份
        Player min = new Player("UZI",0,Poker.licensing());
        Player xiao = new Player("卢本伟", 0,Poker.licensing());
        Player loop = new Player("PDD", 0,Poker.licensing());
        //发完牌之后指定地主专用牌
        quitThreeCards(Poker.indexList);
        //判断玩家的身份
        min = whoIsHost(min);
        xiao = whoIsHost(xiao);
        loop = whoIsHost(loop);
        //打印输出玩家的身份和其手中的牌
        System.out.println(min);
        System.out.println(xiao);
        System.out.println(loop);
    }

    /**
     * 随机生成地主牌，并确保地主牌不在三张底牌中
     * @param list 此处的list指的是剩下三张扑克的下标
     */
    public static void quitThreeCards(List<Integer> list) {
        //生成一个随机数下标，指定地主的牌
        //打印三张底牌
        System.out.print("三张底牌是：");
        for (Integer i: list) {
            System.out.print(Poker.getDek().get(i)+" ");
        }
        System.out.println();


        Random random = new Random();
        for (;;){
            int index = random.nextInt(54);
            for (Integer i :list) {
                if (index == i) {
                    //如果刚好选中了三张底牌之一的下标，则递归重来
                    quitThreeCards(list);
                }else {
                    poker = Poker.getDek().get(index);
                    System.out.println("地主牌是"+poker);
                    return;
                }
            }
        }
    }

    /**
     * 判断谁是地主
     * @param player
     * @return
     */
    public static Player whoIsHost(Player player){

        List<Poker> list = player.getPokerList();

        //若地主牌在当前玩家手中，则此玩家为地主，而后将桌面的三张牌给此玩家
        for (int i = 0; i < list.size(); i++) {
            if (poker.equals(list.get(i))){
                //若是，将玩家的类型设为地主
                player.setType(1);
                ArrayList<Integer> indexList = Poker.getIndexList();
                for (Integer in: indexList) {
                    //将三张牌加入此玩家的手牌
                    list.add(Poker.getDek().get(in));
                }
                //这里的操作是为了使得此玩家手牌有序
                List<Poker> newList = Poker.getDek();
                newList.retainAll(list);
                player.setPokerList(newList);
                return player;
            }
        }
        return player;
    }
}
